package m1;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");  // "wht6.cn"
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();
        Channel c = con.createChannel(); //通信通道

        // 创建队列
        c.queueDeclare("helloworld",false,false,false,null);

        // 回调对象
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            byte[] body = message.getBody();
            String s = new String(body);
            System.out.println("收到："+s);
        };
        CancelCallback cancelCallback = consumerTag -> {};

        // 开始接收消息，消费消息，收到的消息会传递到一个回调对象处理
        // c.basicConsume("helloworld", true, 处理消息的回调对象, 取消处理消息的回调对象);
        c.basicConsume("helloworld", true, deliverCallback, cancelCallback);
    }
}
